﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SyndicateDAO.DatabaseObject;
using SyndicateBusiness.Classes;

namespace BigLotterySyndicate.Secured.Users.Controls
{
    public partial class TabPaymentHistory : Base
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            this.BasePage.PageTitle += " | Account history";

            SetList();
        }

        private void SetList()
        {
            if (Page.IsPostBack)
                return;

            BindList();
        }

        private void BindList()
        {
            var query = this.BasePage.BusinessFacade.FundsLogBL.SelectAllByToUserId(this.CurrentUser.id).OrderByDescending(f => f.dateCreated);

            this.TopPager.RecordCount = this.BottomPager.RecordCount = query.Count();

            if (this.TopPager.RecordCount > 0)
            {
                this.RepeaterList.DataSource = query.Skip(this.TopPager.StartIndex - 1).Take(this.TopPager.PageSize);
                this.RepeaterList.ItemDataBound += new RepeaterItemEventHandler(RepeaterList_ItemDataBound);
                this.RepeaterList.DataBind();

                this.MultiViewList.SetActiveView(this.ViewList);
            }
            else
            {
                this.MultiViewList.SetActiveView(this.ViewEmpty);
            }
        }

        void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            fundsLog u = e.Item.DataItem as fundsLog;

            BigLotterySyndicate.Secured.Controls.PaymentListItem listItem = Utilities.FindControlRecursive(e.Item, "PaymentListItem") as BigLotterySyndicate.Secured.Controls.PaymentListItem;
            if (listItem != null)
                listItem.FundsLogEntry = u;
        }

        protected void Pager_PageIndexChanged(object sender, global::BigLotterySyndicate.Controls.PageIndexChangedEvetArgs e)
        {
            this.TopPager.PageIndex = this.BottomPager.PageIndex = e.PageIndex;

            this.BindList();
        }


    }
}